﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.basicData;
using baseSet.importExport;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess;
using baseSet;
using dhDataAcess.produceLinq;
using dhDataAcess.baseBLL;
using dhSystemBLL.orderData;


namespace dhsystem.produceForm
{
    public partial class FrmcgthckMasterAdd : Form
    {
        purchase.cgthckDetailDataTable cgthckDetailTable = new purchase.cgthckDetailDataTable();
        purchase.cgthckMasterDataTable cgthckMasterTable = new purchase.cgthckMasterDataTable();
        cgthckDetailModel cgthckDetailRow = new cgthckDetailModel();
        cgthckBLL cgthck = new cgthckBLL();
        cgshrkBLL cgshrk = new cgshrkBLL();
        deptBLL dept = new deptBLL();
        workerListBLL workerList = new workerListBLL();
        supplierListBLL supplier = new supplierListBLL();
        cgthckMasterModel cgthckMasterRow = new cgthckMasterModel();
        BindingSource b2;
        string state;
        string stockName;
        public FrmcgthckMasterAdd(BindingSource b1, string _state,string _stockName)
        {
            InitializeComponent();
            state = _state;
            b2 = b1;
            stockName =_stockName ;
        }
        //打开窗口时绑定数据
        private void FrmcgthckMasterAdd_Load(object sender, EventArgs e)
        {
            textstockname.Text =stockName ;
            textbillacount.Text = "0";
            bindingSourcesupplierList.DataSource = supplier.getsupplierListAll();
            comboBoxsNum.DataSource = bindingSourcesupplierList;
            comboBoxsNum.DisplayMember = "sName";
            comboBoxsNum.ValueMember = "sNum";
            bindingSourcedeptList.DataSource = dept.getdeptListAll();
            comboBoxdeptName.DataSource = bindingSourcedeptList;
            comboBoxdeptName.DisplayMember = "deptName";
            comboBoxdeptName.ValueMember = "deptNum";
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";
            //comboBoxwrokernameck.DataSource = bindingSourceworkerList;
            //comboBoxwrokernameck.DisplayMember = "name";
            //comboBoxwrokernameck.ValueMember = "workerNum";
            if (state == "新增")
            {
                textbillNo.Text = "R" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("采购退货单号");
                bindingSourcecgthckDetail .DataSource  = cgthck.getscgthckDetailAllBybillNo(textbillNo.Text);
                dGridDetail.DataSource = bindingSourcecgthckDetail;

            }
            if (state == "编辑")
            {
                textbillNo.DataBindings.Add("text", b2, "billNo");
                comboBoxdeptName.DataBindings.Add("SelectedValue", b2, "deptName");
                comboBoxworkername.DataBindings.Add("SelectedValue", b2, "workername");
                textreturnMark.DataBindings.Add("text", b2, "returnMark");
                dateTimebillDate.DataBindings.Add("value", b2, "billDate");
                textckbillno.DataBindings.Add("text", b2, "ckbillno");
                textbillacount.DataBindings.Add("text", b2, "billacount");
                comboBoxsNum.DataBindings.Add("SelectedValue", b2, "sNum");
                textmark.DataBindings.Add("text", b2, "mark");
                bindingSourcecgthckDetail.DataSource = cgthck.getscgthckDetailAllBybillNo(textbillNo.Text);
                dGridDetail.DataSource = bindingSourcecgthckDetail;
            }
            DcolumnSet.colSet(moduleInfo.cgthckDetail, dGridDetail);
            dGridDetail.AllowUserToAddRows = false;
            if (stockName == "原材料仓")
            {
                comboBoxworkername.Text = "钟三妹";
            }
            else
            {
                comboBoxworkername.Text = "张忠平";
            }
            comboBoxdeptName.Text = "仓储部";
        }
        //int i = 0;
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textbillStatus.Text == "结算")
            {
                MessageBox.Show("此订单已经结算，不能进行修改。如需修改请反结算");
                return;
            }
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输入库单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        dhDataAcess.dataAcess.purchase.cgthckDetailDataTable ptable = (dhDataAcess.dataAcess.purchase.cgthckDetailDataTable)bindingSourcecgthckDetail.DataSource;
                        purchase.cgthckMasterRow cgthckMasterRow = cgthckMasterTable.NewcgthckMasterRow();
                        cgthckMasterRow.billNo = textbillNo.Text;
                        cgthckMasterRow.deptName = comboBoxdeptName.SelectedValue.ToString();
                        cgthckMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        cgthckMasterRow.billDate = dateTimebillDate.Value;
                        cgthckMasterRow.billStatus = "";
                        cgthckMasterRow.ckbillno = textckbillno.Text;
                        cgthckMasterRow.returnMark = textreturnMark.Text;
                        cgthckMasterRow.billacount = Convert.ToDecimal(textbillacount.Text);
                        cgthckMasterRow.sNum = comboBoxsNum.SelectedValue.ToString();
                        cgthckMasterRow.createDate = DateTime.Now;
                        cgthckMasterRow.createUser = GlobalInfo.UserName;
                        cgthckMasterRow.updateDate = DateTime.Now;
                        cgthckMasterRow.updateUser = GlobalInfo.UserName;
                        cgthckMasterRow.mark = textmark.Text;
                        cgthckMasterRow.stockname = textstockname.Text;
                        cgthckMasterTable.AddcgthckMasterRow (cgthckMasterRow);
                        cgthckMasterTable.AcceptChanges();
                        foreach (dhDataAcess.dataAcess.purchase.cgthckDetailRow  a in ptable)
                        {
                            if (!a.IsfqtyNull())
                            {
                                a.fqty = a.fqty;
                            }
                            else { a.fqty = 0; }
                            var s = ptable.Where(x => x.matNum == a.matNum).ToList();
                            if (s.Count() > 1)
                            {
                                MessageBox.Show("存在两条相同产品名称的记录，不能保存");
                                return;
                            }
                            if (!a.IsqtyNull())
                            {
                                a.qty = a.qty;
                            }
                            else
                            {
                                DialogResult sresult = MessageBox.Show("数量为空是否保存！", "提示信息", MessageBoxButtons.YesNo);
                                if (sresult == DialogResult.Yes)
                                {
                                    a.qty = 0;
                                }
                                if (sresult == DialogResult.No)
                                {
                                    MessageBox.Show("保存已取消");
                                    return;
                                }
                            }
                        }
                        cgthck.cgthckInsert(cgthckMasterTable, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourcecgthckMaster.DataSource = cgthck.getcgthckMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //删除子件
        private void buttDel_Click(object sender, EventArgs e)
        {

            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //编辑子件
        private void buttedit_Click(object sender, EventArgs e)
        {

            FrmcgthckDetailAdd cgthckDetail = new FrmcgthckDetailAdd(bindingSourcecgthckDetail , "编辑", textbillNo.Text);
            cgthckDetail.ShowDialog();
        }
        //添加子件
        private void buttAdd_Click(object sender, EventArgs e)
        {
            FrmcgthckDetailAdd cgthckDetail = new FrmcgthckDetailAdd(bindingSourcecgthckDetail, "新增", textbillNo.Text);
            cgthckDetail.ShowDialog();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string field;
            field = "billNo";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.cgshrkMaster , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textckbillno.Text = productselect.pname;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string field;
            field = "workerNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxworkername.SelectedValue = productselect.pname;
            }
        }

 

        private void button2_Click(object sender, EventArgs e)
        {
            string field;
            field = "sNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.supplierList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxsNum.SelectedValue = productselect.pname;
            }
        }

        private void buttuserselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "deptNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.deptList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxdeptName.SelectedValue = productselect.pname;
            }
        }

        private void textckbillno_TextChanged(object sender, EventArgs e)
        {
            purchase.cgshrkDetailDataTable cgshrkTable = cgshrk.getscgshrkDetailAllBybillNo(textckbillno.Text);

            foreach (purchase.cgshrkDetailRow  cgshrkDetailRow in cgshrkTable)
            {
                purchase.cgthckDetailRow cgthckRow = cgthckDetailTable.NewcgthckDetailRow();
                cgthckRow.acount = cgshrkDetailRow.acount;
                cgthckRow.billNo = textbillNo.Text;
                cgthckRow.createDate = DateTime.Now;
                cgthckRow.currency = cgshrkDetailRow.currency;
                cgthckRow.qty = cgshrkDetailRow.qty;
                cgthckRow.price = cgshrkDetailRow.price;
                cgthckRow.fqty = cgshrkDetailRow.fqty;
                cgthckRow.funit = cgshrkDetailRow.funit;
                cgthckRow.unit = cgshrkDetailRow.unit;
                cgthckRow.matNum = cgshrkDetailRow.matNum;
                cgthckRow.matName = cgshrkDetailRow.matName;
                cgthckRow.matSpec = cgshrkDetailRow.matSpec;
                cgthckRow.mark = cgshrkDetailRow.mark;
                cgthckDetailTable.AddcgthckDetailRow(cgthckRow);
            }
            bindingSourcecgthckDetail.DataSource = cgthckDetailTable;
            dGridDetail.DataSource = bindingSourcecgthckDetail;
        }

        private void groupAdd_Enter(object sender, EventArgs e)
        {

        }
    }
}
